<?php
class CMRPageShowResource extends CMRPage
{
	protected $resourceID;
	
	public function __construct($database, $ResourceID)
	{
		parent::__construct($database);
		$this->resourceID = $ResourceID;
	}
	
	protected function getHTTPHeadersHTML()
	{
		$sql = "SELECT * from Resources WHERE ResourceID=".$_POST['ResourceID'];
		$this->Database->ExecuteSQL($sql);
		$row = $this->Database->getRow();
		$resource = new Resource($this->Database->Recopy(),$row['ResourceID']);
//		$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
		
//		echo $row['ResourceTypeID'];
		// if a link, redirect to site
		if ($row['ResourceTypeID']==7)
		{
			$sql = "SELECT * from Links WHERE ResourceIDP=".$_POST['ResourceID'];
			$this->Database->ExecuteSQL($sql);
			$row = $this->Database->getRow();
			
			header("Location: ".$row['URL']);
			exit();
		}
		
		if ($row['ResourceTypeID']==6)
		{
		
			$sql = "SELECT FSFileName,FSFileSize,FSFileType FROM Files WHERE ResourceIDP = ".$this->resourceID;
//			echo $sql."<br>";
			$this->Database->ExecuteSQL($sql);
				
			list($name, $type, $size) =  $this->Database->getRow(); 
//			echo $name." ".$type." ".$size." ".$recordID."<br>";
			$filename = "ResourceFiles/Files_FSFile_".$this->resourceID;
			$content = file_get_contents($filename);
	
//			header("Content-length: $size");
			header("Content-type: $type");
			header("Content-Disposition: attachment; filename=\"$name\"");
			echo $content;
				
			$this->Database->close();
			exit;
		}
	}

	protected function getContentsDiv()
	{
		
		$sql = "SELECT * from Resources WHERE ResourceID=".$_POST['ResourceID'];
		$this->Database->ExecuteSQL($sql);
		$row = $this->Database->getRow();
		
		// is this necessary ....
//		$resource = new Resource($this->Database->Recopy(),$row['ResourceID']);
//		$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
		
		$html = "<div id=\"content\">\n";
		$html .= "<h1>".$row['ResourceTitle']."</h1>\n";
		
/*		$html .= "<p style=\"text-align: center;\">\n";
		
		$html .= "<img id=\"ImgChoral\" width=\"130\" height=\"80\" ";
		$html .= "onMouseOver=\"OnMouseOverChoral();\" ";
		$html .= "onMouseOut=\"OnMouseOutChoral();\" ";
		$html .= "onclick=\"OnClickChoral();\" ";
		$html .= "src=\"../Images/Choral";
		$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "<img id=\"ImgOrgan\" width=\"130\" height=\"80\" ";
		$html .= "onMouseOver=\"OnMouseOverOrgan();\" ";
		$html .= "onMouseOut=\"OnMouseOutOrgan();\" ";
		$html .= "onclick=\"OnClickOrgan();\" ";
		$html .= "src=\"../Images/Organ";
		$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "<img id=\"ImgLiturgy\" width=\"130\" height=\"80\" ";
		$html .= "onMouseOver=\"OnMouseOverLiturgy();\" ";
		$html .= "onMouseOut=\"OnMouseOutLiturgy();\" ";
		$html .= "onclick=\"OnClickLiturgy();\" ";
		$html .= "src=\"../Images/Liturgy";
		$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "<img id=\"ImgProfessional\" width=\"130\" height=\"80\" ";
		$html .= "onMouseOver=\"OnMouseOverProfessional();\" ";
		$html .= "onMouseOut=\"OnMouseOutProfessional();\" ";

		$html .= "onclick=\"OnClickProfessional();\" ";
		$html .= "src=\"../Images/Professional";
		$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "</p>\n";	*/

		$sectionsPanel = new SectionsPanel('');
		$html .= $sectionsPanel->getHTML();
		
		
		$html .= "<hr>\n";		
		
		if(($this->userGroup=="Editors")||($this->userGroup=="Supervisors"))
		{
			$hidden = new Hidden();
			$hidden->setName("SubAction");
			$html .= $hidden->getHTML();
		
			$hidden = new Hidden();
			$hidden->setName("ResourceID");
			$html .= $hidden->getHTML();
		
			$button = new Button();
			$button->setName("btEdit");
			$button->setValue("Edit");
			$button->setOnclick("OnClickEditResource(".$_POST['ResourceID'].")");
			
			$html .= $button->getHTML();
		}
		
		
		switch ($row['ResourceTypeID'])
		{
			case 2: // Repertoire item
				$resource = new ResourceRepertoireInfo($this->Database->Recopy(),$row['ResourceID']);
				$html .= $resource->getDataHTML();
				$html .= $resource->getFullHTML();
				break;
				
			case 3: // Composer
				$resource = new ResourceComposer($this->Database->Recopy(),$row['ResourceID']);
				$html .= $resource->getDataHTML();
				$html .= $resource->getFullHTML();
				break;
				
			case 6: // File
			case 7: // link
			default:
				break;
		}
				
		$html .= "</div>\n";
		return $html;
	}
	
}